﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Look_Through_Windows.Helper
{
    public delegate bool CallBackPtr( IntPtr hwnd, int lParam );

    [StructLayout( LayoutKind.Sequential )]
    public struct RECT
    {
        public int Left;        // x position of upper-left corner
        public int Top;         // y position of upper-left corner
        public int Right;       // x position of lower-right corner
        public int Bottom;      // y position of lower-right corner
    }
    [StructLayout( LayoutKind.Sequential )]
    public struct WINDOWINFO
    {
        public int cbSize;
        public RECT rcWindow;
        public RECT rcClient;
        public int dwStyle;
        public int dwExStyle;
        public int dwWindowStatus;
        public uint cxWindowBorders;
        public uint cyWindowBorders;
        public int atomWindowType;
        public int wCreatorVersion;
    }

    /// <summary>
    /// Helper class for extracting window content
    /// </summary>
    public static class WindowExtract
    {
        [DllImport( "WindowExtract.dll", CallingConvention = CallingConvention.Cdecl )]
        public static extern IntPtr ExtractAllocate( IntPtr hwnd, ref int width, ref int height );

        [DllImport( "WindowExtract.dll", CallingConvention = CallingConvention.Cdecl )]
        public static extern void ExtractDeallocate( IntPtr buffer );

        [DllImport( "WindowExtract.dll", CallingConvention = CallingConvention.Cdecl )]
        public static extern IntPtr XOR( IntPtr buffer1, IntPtr buffer2, int sizeInElements );

        [DllImport( "user32.dll" )]
        public static extern int EnumWindows( CallBackPtr callPtr, int lPar );

        [DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
        public static extern int GetWindowText( IntPtr hWnd, StringBuilder lpString, int nMaxCount );

        [DllImport( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )]
        public static extern int GetWindowTextLength( IntPtr hWnd );

        [DllImport( "user32.dll" )]
        [return: MarshalAs( UnmanagedType.Bool )]
        public static extern bool GetWindowRect( IntPtr hwnd, out RECT lpRect );

        [DllImport( "user32.dll" )]
        [return: MarshalAs( UnmanagedType.Bool )]
        public static extern bool IsWindowVisible( IntPtr hWnd );
        [DllImport( "user32.dll" )]
        [return: MarshalAs( UnmanagedType.Bool )]
        public static extern bool IsWindow( IntPtr hWnd );

        [return: MarshalAs( UnmanagedType.Bool )]
        [DllImport( "user32.dll", SetLastError = true )]
        public static extern bool GetWindowInfo( IntPtr hwnd, ref WINDOWINFO pwi );

        [DllImport( "user32.dll" )]
        public static extern bool GetClientRect( IntPtr hWnd, out RECT lpRect );

        [DllImport( "user32.dll" )]
        public static extern bool ClientToScreen( IntPtr hWnd, ref Point lpPoint );

        [DllImport( "msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false )]
        public static extern IntPtr memcpy( IntPtr dest, IntPtr src, UIntPtr count );
    }
}
